#include "cJSON/cJSON.h"
#include <stdio.h>
#include "log/log.h"

int main(int argc, char const *argv[])
{
    //生成json字符串
    cJSON *root = cJSON_CreateObject();
    //向json中添加数据
    cJSON_AddStringToObject(root, "name", "zhangsan");
    cJSON_AddNumberToObject(root, "age", 18);
    cJSON_AddStringToObject(root, "sex", "male");
    // char *json_str = cJSON_Print(root);
    // log_debug("json_str:%s", json_str);
    char *json_str2 = cJSON_PrintUnformatted(root);
    log_debug("json_str2:%s", json_str2);

    //解析json字符串
    cJSON *json = cJSON_Parse(json_str2);
    //获取json中的数据
    char *name = cJSON_GetObjectItem(json, "name")->valuestring;
    int age = cJSON_GetObjectItem(json, "age")->valueint;
    char *sex = cJSON_GetObjectItem(json, "sex")->valuestring;
    log_debug("name:%s, age:%d, sex:%s", name, age, sex);

    //释放内存
    cJSON_Delete(root);
    cJSON_Delete(json);
    return 0;
}
